/************************************  

DESCRIPTION: This is the ols regression run separately for men/women on the full sample.
 5-digit zip codes interacted with race controls. This is death. Hypertension/Diabetes done separately
 
 Datasets used: 
  (1) maleLPManalysis2
  (2) femLPManalysis2

 Datasets created: 
  (1-2) fembetaDeath malebetaDeath 
  
 ************************************/  

set more off
capture log close
clear all

set matsize 10000

global origData "N:\MedicareClaims-P045601-BE"
global dataIn "N:\MedicareClaims-P045601-BE\Work\hosp_retro\health_out\Data-In\"
global dataOut "N:\MedicareClaims-P045601-BE\Work\hosp_retro\health_out\Data-Out"
global dpath "N:\MedicareClaims-P045601-BE\Work\ay_data"
global dataProp "N:\MedicareClaims-P045601-BE\Work\hosp_retro\health_out\Data-Out\PropScore\Patients"
global logs "N:\MedicareClaims-P045601-BE\Work\hosp_retro\health_out\Logs\PropScore\Patients"
global dpath "N:\MedicareClaims-P045601-BE\Work\ay_data"
global skapath "N:\MedicareClaims-P045601-BE\Work\ska"
global output

adopath +  "N:/SIL-Common/estout"
adopath +  "N:/SIL-Common/outreg2"
adopath +  "N:/SIL-Common/reghdfe-master/package"

log using "$logs/3LPMdeath.log", replace


*Columnames program

	program getColnames 
	 quietly {
	  matrix A = e(b)'
	 
	 local rownames : rowfullnames A
	 local c : word count `rownames'
	 local names : colfullnames A

	 svmat A, names(col)

	 gen rownames = ""
	 forvalues i = 1/`c' {

	  replace rownames = "`:word `i' of `rownames''" in `i' 
	   } 

	 }

	end


	program getRegStats
	 quietly {
	  matrix A = e(b)'
	 
	 local rownames : rowfullnames A
	 local c : word count `rownames'
	 local n = `c' + 1
	 local rsq = `c' + 3

	 replace beta1 =e(N) in `n' 
	 replace beta1 =e(r2_p) in `rsq' 

	 }

	end


 *Run Linear probability analysis
		 foreach looper in "male" "fem" {
			  use `looper'LPManalysis2.dta
			   capture drop zip3
				 gen zip3 = substr(zip_pat,1,3)
				 encode zip3, gen(zip3n)
			   capture drop urban_rural
			   gen urban_rural=urbanrural1+2*urbanrural2
				replace urban_rural=3 if urban_rural==0
				 capture rename mxicd8_chronic mxicd8_cardiac
			 capture drop spec35 spec97 spec66 spec04 spec16 spec05 spec43 spec08 spec01 spec11 spec93 spec83 spec39 spec66 spec04 spec16 
			 capture drop specrt
			 
		*All
				 foreach var of varlist oc_death {
					 if regexm("`var'","oc_([a-z0-9]+)$") { 
					  local pre = "`=regexs(1)'"
					 }
		 
				 di "`pre'"
				 
				 capture rename mxicd3_chronic mxicd3_endocrine 
				 capture rename mxicd3_endocrine mxicd3_chronic 
				 capture rename mxicd8_chronic mxicd8_cardiac
				 
				areg `var' i.postmerger  i.vmerger  i.vmerger#c.nyear i.age##i.rac_white i.qtr##i.urban_rural mxicd1_acute mxicd5_acute mxicd18_acute mxicd*_ch* fmp nptf anesth spec* fs* lnvisit* if  kv_`pre'==1, abs(zipwhite) cluster(zip3n)   
				matrix beta  = e(b)' 
				  matrix sesq = vecdiag(e(V))' 
				  svmat beta
				  getColnames
				  getRegStats
				  svmat sesq
				  rename rownames rn`pre'_`i'
				  capture drop y1 
				  capture drop j 
				  capture drop i
				  rename beta1 beta_`pre'_`i'
				  rename sesq1 sesq_`pre'_`i'
				 
				 
				  matrix B = e(b)
				  local nvars = colsof(B)
				 
				  replace rn`pre'_`i' = "N" if _n == `nvars' + 1
				  replace beta_`pre'_`i' = e(N) if _n == `nvars' + 1
				 
				  replace rn`pre'_`i' = "R2" if _n == `nvars' + 2
				  replace beta_`pre'_`i' = e(r2) if _n == `nvars' + 2
				 
				  replace rn`pre'_`i' = "aR2" if _n == `nvars' + 3
				  replace beta_`pre'_`i' = e(r2_a) if _n == `nvars' + 3
				 
				 }
 
			 keep rn* beta* sesq*
			 drop if _n>1000
		 save ./`looper'betaDeath.dta, replace
		 clear
		 
		}
